﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq; 

namespace CMTVision.Widgets.Impls
{
    public class Template:System.Web.UI.UserControl,CMTVision.Widgets.Common.ITemplate
    {
        #region Ctor

        public Template()
            : base()
        {
            IsInEditMode = false;
        }

        #endregion

        #region Implement ITemplate interface

        public int TemplateDataID
        {
            get;
            set;
        }

        public bool IsInEditMode
        {
            get
            {
                return ViewState["IsInEditMode"] == null ? false : Convert.ToBoolean(ViewState["IsInEditMode"]);
            }
            set
            {
                ViewState["IsInEditMode"] = value;
            }
        }

        public void LoadTemplateColumns()
        {
            if (TemplateDataID > 0)
            {
                var templateData = (from td in XDocument.Load(Server.MapPath("~/Data/templatedata.xml")).Element("templatedatas").Elements("templatedata")
                                    where td.Element("id").Value == TemplateDataID.ToString()
                                    select td).FirstOrDefault();
                if (templateData != null)
                {
                    var columns = (from c in templateData.Element("columns").Elements("column")
                                   select c).ToList();
                    foreach (var column in columns)
                    {
                        var columnId = column.Attribute("id").Value;
                        var columnControl = FindControl(columnId) as CMTVision.Widgets.Common.ITemplateColumn;
                        //columnControl.Template = this;
                        //columnControl.WidgetContainerAction = w => {
                        //    _WidgetControlMap.Add(w.UniqueID, w);
                        //};
                        columnControl.ColumnID = columnId; 
                        columnControl.TemplateDataID = TemplateDataID;
                        columnControl.IsInEditMode = IsInEditMode;
                        columnControl.LoadWidgets();
                    }
                }
            }
        }

        public virtual void ForceRefresh()
        {
            if (TemplateDataID > 0)
            {
                var templateData = (from td in XDocument.Load(Server.MapPath("~/Data/templatedata.xml")).Element("templatedatas").Elements("templatedata")
                                    where td.Element("id").Value == TemplateDataID.ToString()
                                    select td).FirstOrDefault();
                if (templateData != null)
                {
                    var columns = (from c in templateData.Element("columns").Elements("column")
                                   select c).ToList();
                    foreach (var column in columns)
                    {
                        var columnId = column.Attribute("id").Value;
                        var columnControl = FindControl(columnId) as CMTVision.Widgets.Common.ITemplateColumn;
                        if (columnControl != null)
                        {
                            columnControl.IsInEditMode = IsInEditMode;
                            columnControl.ForceRefresh();
                        }
                    }
                }
            }
        }

        #endregion


        
    }
}